package com.SFA.OrderManagement;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

import com.FrameWork.Common.AbstractBaseActivity;
import com.FrameWork.Common.DRTL_ListViewUtil;
import com.FrameWork.Common.DRTL_ListViewUtil.IXListViewListener;
import com.FrameWork.Entity.CustomerInfoEntity;
import com.FrameWork.Entity.OrderEntity;
import com.SFA.R;
import com.SFA.Customer.CustomerManager;

public class OrderListActivity extends AbstractBaseActivity implements IXListViewListener,OnQueryTextListener,ActionBar.TabListener{

	private DRTL_ListViewUtil  ListView;
	List<CustomerInfoEntity> list=null;
	private int PageSize=10,PageIndex=0;
	private String KeyWord="";
	List<OrderEntity> orderList; 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
		searchView.setOnQueryTextListener(this);
		searchView.setActivated(true);
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.search:
			/*if (mThemeId == R.style.AppTheme_Dark) {
				mThemeId = R.style.AppTheme_Light;
			} else {
				mThemeId = R.style.AppTheme_Dark;
			}
			this.recreate();*/
			return true;


		default:
			return super.onOptionsItemSelected(item);
		}

	}



	private void menuToAddMethod() {
		list = new CustomerManager(this).getCustomerList();
		String[] str=new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			str[i]=list.get(i).NAME;
		}
		if(list.size()==1){
			Intent intent = new Intent(context, SelectProductActivity.class);
			intent.putExtra("CUSTOMER", list.get(0));
			intent.putExtra("SOURCE", "NEW");
			startActivityForResult(intent, 100);

		}else if(list.size()>1){
			new AlertDialog.Builder(this).setSingleChoiceItems(str,0, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					CustomerInfoEntity customer=list.get(which);
					Intent intent = new Intent(OrderListActivity.this, OrderDetailActivity.class);
					intent.putExtra("CUSTOMER", customer);
					OrderListActivity.this.startActivityForResult(intent, 100);

				}
			}).setNegativeButton("取消", null)
			.create().show();

		}
	}





	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		ListView = new DRTL_ListViewUtil(this);
		ListView.setXListViewListener(this);
		ListView.setDivider(this.getResources().getDrawable(R.drawable.solid_line));
		ListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
		ListView.setVerticalScrollBarEnabled(false);
		addContentView(ListView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

		ActionBar bar = getActionBar();
		bar.hide();
		Tab tab = bar.newTab().setText("新订单")
				.setTabListener(this);
		tab.setTag("ADD");
		bar.addTab(tab);
		bar.selectTab(null);
		bar.show();
	}

	public void BindTodayList(String keyword)
	{
		if(!KeyWord.equals(keyword ))
		{
			KeyWord =keyword;
			PageIndex=0;
		}
		List<OrderEntity> tempdata= new OrderManager().QueryOrder(context,keyword,PageSize,PageIndex);
		
		if(orderList == null || PageIndex ==0)
		{
			System.gc();
			orderList = new ArrayList<OrderEntity>();
			ListView.setPullLoadEnable(true);
		}
		if(tempdata != null)
		{
			for(OrderEntity entity:tempdata)
			{
				orderList.add(entity);
			}
		}
		
		if(tempdata == null || tempdata.size()==0 || tempdata.size()< PageSize)
			ListView.setPullLoadEnable(false);
		else
			ListView.setPullLoadEnable(true);
		ListView.setSelection(ListView.position);
		OrderAdapter adapter = new OrderAdapter(orderList,context);
		ListView.setAdapter(adapter);
		ListView.LastRefreshTime = ListView.df.format( new Date());
		ListView.stopLoadMore();
		ListView.stopRefresh();
		tempdata = null;
		PageIndex++;
	}

	@Override
	protected void onStart() {
		super.onStart();
		onRefresh();
		ActionBar bar = getActionBar();
		bar.selectTab(null);
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		if(tab==null)
			return;
		ActionBar bar = getActionBar();
		if(!bar.isShowing())
			return;

		if("ADD".equals(tab.getTag()))
		{
			menuToAddMethod();
		}


	}



	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}


	@Override
	public boolean onQueryTextChange(String text) {
		//查找满足条件的订单
		//BindTodayList(text);
		return false;
	}


	@Override
	public boolean onQueryTextSubmit(String query) {
		//查找满足条件的订单
		BindTodayList(query);
		return false;
	}


	@Override
	public void onRefresh() {
		PageIndex=0;
		BindTodayList(this.KeyWord);

	}


	@Override
	public void onLoadMore() {
		BindTodayList(this.KeyWord);

	}





}
